/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package mx.llantas.vista;

import java.util.List;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.event.ActionEvent;
import mx.llantas.Bl.VendedorblLocal;
import mx.llantas.modelo.Vendedor;

/**
 *
 * @author manuel
 */
@ManagedBean
@SessionScoped
public class VendedorBean {
    
    @EJB
    private VendedorblLocal vendedorbl;

    //------------variables----------------
  private List<Vendedor> listaVendedores;
  private Vendedor salesman;
  private String accion;
  //---------constructor-----
   public VendedorBean(){
   salesman= new Vendedor();
   }

    public Vendedor getSalesman() {
        return salesman;
    }

    public void setSalesman(Vendedor salesman) {
        this.salesman = salesman;
    }
     public List<Vendedor> getLista() {
        listaVendedores=vendedorbl.getAll();
        System.out.println(listaVendedores);        
        return listaVendedores;
    }
    //--------acciones-------
    public String procesarAccion(){
        System.out.println("entro a procesar");
                
    if(accion.equalsIgnoreCase("Nuevo")){
    vendedorbl.register(salesman);
        System.out.println("entro a nuevo"+accion);
    }else{
    if(accion.equalsIgnoreCase("Editar")){
    vendedorbl.ModiFy(salesman);
    }else{
    if(accion.equalsIgnoreCase("Eliminar")){
        vendedorbl.delete(salesman);
        }
    }
    }
    accion="";
    return "ListaVendedores";

}    
    //-------------listeners----------------
public void prepararNuevo(ActionEvent evt){
    salesman= new Vendedor(); 
    accion="nuevo";
}
public void prepararEditar(ActionEvent evt){
       System.out.println(evt.getComponent().getClientId());
       int index = Integer.parseInt(evt.getComponent().getClientId().split(":")[2]);
       System.out.println("el numero es:"+ index);
       salesman=listaVendedores.get(index);
       accion="Editar";               
}
public void prepararEliminar(ActionEvent ev){
    int index = Integer.parseInt(ev.getComponent().getClientId().split(":")[2]);      
       salesman=listaVendedores.get(index);
accion="Eliminar";
}

  
   
  

}
